package com.dzl.util.wx;

import org.apache.commons.lang3.StringUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Random;
import java.util.zip.ZipOutputStream;

/**
 *
 * @Project: shop
 * @Title: FileUtil.java
 * @Package com.yome360.msd.util
 * @Description: 文件工具类
 *
 * @author lujianmin ljm_3331@126.com
 * @date 2013-9-5 下午3:04:33
 * @Copyright: 2013 武汉由米定制科技有限公司 All rights reserved.
 * @version V1.0
 */
public class FileUtil {

	/**
	 *
	 * @Title: getExtName
	 * @Description: 获取文件扩展名
	 * @param filename
	 * @return
	 */
    public static String getExtName(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot >-1) && (dot < (filename.length() - 1))) {
                return filename.substring(dot + 1);
            }
        }
        return "";
    }

    /**
     *
     * @Title: validExt
     * @Description: 判断文件后缀名是否合法
     * @param ext 文件后缀
     * @return
     */
    public static boolean validExt(String ext){
    	String[] arr ={"jpg","png"};
    	for (String s : arr) {
			if (s.equalsIgnoreCase(ext)) {
				return true;
			}
		}
    	return false;
    }

	/**
	 *
	 * @Title: getFileNameNoEx
	 * @Description: 获取不带扩展名的文件名
	 * @param filename
	 * @return
	 */
    public static String getFileNameNoEx(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot >-1) && (dot < (filename.length()))) {
                return filename.substring(0, dot);
            }
        }
        return "";
    }

    /**
     *
     * @Title: getImgPix
     * @Description: 获取图片的像素
     * @param file
     * @return
     */
    public static int[] getImgPix(File file){
    	InputStream is = null;
    	BufferedImage src = null;
    	try {
    		is = new FileInputStream(file);
    		//构造Image对象
    		src = ImageIO.read(is);
    		if (src!=null) {
    			return new int[]{src.getTileWidth(), src.getTileHeight()};
			}
    	} catch (FileNotFoundException e) {
    		e.printStackTrace();
    	} catch (IOException e) {
    		e.printStackTrace();
    	} finally {
    		if (is!=null) {
    			try {
    				is.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    	return new int[]{0, 0};
    }

    /**
     * @param isPic
     * @Description  获取新的文件名  命名格式：yyyyMMddHHmmssSSS_三位随机数
	 * @param root  根目录
	 * @param fileExt 后缀名
	 * @return
	 */
	public static String getNewFileName(String fileName, boolean isPic,String fileExt){
		SimpleDateFormat df = new SimpleDateFormat("yyMMddHHmmssSSS");
		Integer random = new Random().nextInt(899)+100;
		String fullPath = "";
		if(isPic){
			fullPath = df.format(new Date())+random + "." + fileExt;
		}else{
			fullPath = df.format(new Date())+random + "_" + fileName;
		}
		return fullPath;
	}


	public static  FileInputStream getFileInputStreamByFilePath(String filePath){
		if(StringUtils.isBlank(filePath)){
			return null;
		}
		FileInputStream fileInputStream=null;
		try {
			 fileInputStream=new FileInputStream(filePath);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		return  fileInputStream;
	}

	/***
	*  根据图片地址获取流
	* @date 2021/5/18 17:37
	* @author kewenfeng
	* @param url 图片地址
	* @return java.io.InputStream
	*/
	public static InputStream getInputStreamByGet(String url) {
		try {
			HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
			conn.setReadTimeout(5000);
			conn.setConnectTimeout(5000);
			conn.setRequestMethod("GET");

			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				InputStream inputStream = conn.getInputStream();
				return inputStream;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static boolean isOSLinux() {
		Properties prop = System.getProperties();
		String os = prop.getProperty("os.name");
		if (os != null && os.toLowerCase().contains("linux")) {
			return true;
		} else {
			return false;
		}
	}

	/***
	 *  把流输出到压缩包中
	 * @date 2021/5/18 17:40
	 * @author kewenfeng
	 * @param zos 压缩包
	 * @param inputStream 流
	 * @return void
	 */
	public static void writeStream(ZipOutputStream zos, InputStream inputStream) throws IOException {
		byte[] buffer = new byte[1024];
		int r = 0;
		while ((r = inputStream.read(buffer)) != -1) {
			zos.write(buffer, 0, r);
		}
		inputStream.close();
	}
}
